/**
 * $Revision: 1.2 $
 * $Date: 2007/06/09 12:13:06 $
 *
 * Copyright (C) 1999-2005 Jive Software. All rights reserved.
 * This software is the proprietary information of Jive Software. Use is subject to license terms.
 */

package com.regal.newspush;

//import org.jivesoftware.resource.SparkRes;
import javax.swing.SwingUtilities;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.plugin.Plugin;

import com.regal.packet.NewsPush;
import com.regal.packet.NewsPushProvider;

/**
 *
 */
public class NewsPushPlugin implements Plugin {


    public void initialize() {
    	System.out.println("newsPushPlugin added");
        ProviderManager.getInstance().addExtensionProvider("query", "jabber:message:news_push", new NewsPushProvider());
        Message message = new Message();
        message.addExtension(new NewsPush());
        SparkManager.getConnection().addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {
                if (packet instanceof Message) {
                    final Message message = (Message)packet;
                    if (message.getExtension("query", "jabber:message:news_push") != null) {
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                showPubNews(message);
                            }
                        });
                    }
                }
            }
        }, new PacketTypeFilter(Message.class));

     
    }

    private void showPubNews(Message message) {
    	NewsPush newsPacket = (NewsPush)message.getExtension("query", "jabber:message:news_push");
    	System.out.println("news count is:"+newsPacket.getNewsItemCount());
    	SparkManager.getWorkspace().getStatusBar().setNews(newsPacket.getNewsItems());
    }

    public void shutdown() {
    }

    public boolean canShutDown() {
        return true;
    }

    public void uninstall() {
    }
}
